#!/usr/bin/bash
#这是快手自动浏览程序
#原理就是利用adb获取布局，来模拟滑屏点击小红心
APPNAME="快手极速版"
APP_PACKAGE_NAME="com.kuaishou.nebula"
APP_ACTIVITY_NAME="com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity"
exit_game()
{
	echo "${RED}当前界面非${APPNAME}，等待9秒后退出应用${RST}"
	sleep 9
	exit 0
}
RST="$(tput sgr0)"
RED="${RST}$(tput setaf 1)"
BRED="${RST}$(tput bold)$(tput setaf 1)"
GREEN="${RST}$(tput setaf 2)"
YELLOW="${RST}$(tput setaf 3)"
BYELLOW="${RST}$(tput bold)$(tput setaf 3)"
BLUE="${RST}$(tput setaf 4)"
CYAN="${RST}$(tput setaf 6)"
BCYAN="${RST}$(tput bold)$(tput setaf 6)"
ICYAN="${RST}$(tput sitm)$(tput setaf 6)"

clear
runadb=$(adb connect localhost:5555 2>&1)
#echo runadb: ${runadb} \n\n
error_info="error:"
refused_info="refused"
if_contains_error_info=$(echo ${runadb} | grep "${error_info}") 
if_contains_refused_info=$(echo ${runadb} | grep "${refused_info}")
if [[ "$if_contains_error_info" != "" ]] ||  [[  "$if_contains_refused_info" != "" ]]
then 
	echo ${RED}\本机未开启网络调试模式,请按下面步骤进行开启：${RST} 
	echo
	echo ${GREEN}\步骤1，将手机设置成为usb调试模式${RST} 
	echo ${GREEN}\方法：百度关键字\"安卓手机系统怎样打开USB调试模式\"${RST} 
	echo ${GREEN}\进入关于手机-\>点击版本号7次，即可打开开发者选项${RST} 
	echo ${GREEN}\进入开发者选项 打开usb 调试 华为机型还应打开“仅充电”模式下充许adb调试${RST} 
	echo
    echo ${GREEN}\步骤2，连接电脑后运行 adb tcpip 5555${RST} 
	echo ${GREEN}\提示 \"restarting in TCP mode port: 5555\" 就表明已经开启网络调试模式${RST} 
	echo
	echo ${BLUE}\到重启手机之前，都可以正常使用本程序 ${RST} 
	echo ${BLUE}\在正常使用中，最小化${APPNAME}后即可停止本程序 ${RST} 
	exit_game
fi
#启动${APPNAME}界面
adb -s localhost:5555 shell am start ${APP_ACTIVITY_NAME} >/dev/null 2>&1 
sleep 2
#当前界面是否是快手极速版排行榜
is_app_activity=0
#检查当前界面是否是快手极速版排行榜
check_is_app_activity()
{

currentActivity=$(adb -s localhost:5555 shell dumpsys activity | grep ActivityRecord |head -n 2)
key=${APP_ACTIVITY_NAME}
if [[ $currentActivity == *$key* ]]
then 	
	#echo is wechat
	is_app_activity=1
else
	is_app_activity=0
	
fi
}
#检查当前界面是否是快手极速版排行榜
pull_current_activity_xml()
{

check_is_app_activity
if [[ is_app_activity -eq 1 ]] 
then  
	
	#获取当前界面的ui.xml
	adb  -s localhost:5555  shell uiautomator dump /sdcard/ui.xml >/dev/null 2>&1
	adb  -s localhost:5555  pull /sdcard/ui.xml  >/dev/null 2>&1
fi
}


check_is_app_activity
if [[ is_app_activity -eq 1 ]]
then 
	for ((m=0; m<=5000; m++))
	do		 
		#pull_current_activity_xml 
		#readXmlandClick ${m} 
		check_is_app_activity
		if [[ is_app_activity -eq 1 ]] 
		then 
			echo "${GREEN}滑动屏幕一次${RST} "
			adb  -s localhost:5555 shell input swipe 300 600 310 100
			echo "${GREEN}休眠30秒${RST} "
			for ((i=0; i<10; i++))
				do
				check_is_app_activity
				if [[ is_app_activity -eq 1 ]] 
				then 
					sleep 3
				else
					exit_game
				fi
			done
		else
			exit_game
		fi
	done
	echo "${GREEN}\"退出 for \"${RST} "
else
	exit_game
fi